﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

public partial class UserControl_ControlUser : System.Web.UI.UserControl
{
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    ClassExecute.ClassExecute dt;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            //nằm trong thread
            ThreadStart ths1 = new ThreadStart(GetBrandList);
            ThreadStart ths2 = new ThreadStart(GetUserType);
            Thread th1 = new Thread(ths1);
            Thread th2 = new Thread(ths2);
            th1.Start();
            th2.Start();

            th1.Join();
            th2.Join();
            //nằm trong thread
        }
    }
    void ShowMessageBox(string text)
    {
        MessageBox.Text = text;
        MessageBox.ShowOnPageLoad = true;
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindGridSearch();
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        int i = 0;
        try
        {
           
          
                i = dt.ExecuteSQl_int(str, "k_updateUserSystem", CommandType.StoredProcedure, ref error,
                   new SqlParameter("@UserName", txtUserName.Text),
                   new SqlParameter("@UserDescription", txtName.Text),
                   new SqlParameter("@Password", dt.GetMd5Sum(txtPass.Text)),
                   new SqlParameter("@Sitecode", ddlChiNhanh.SelectedValue.ToString()),
                   new SqlParameter("@UserType", ddlThuocNhom.SelectedValue.ToString()),
                   new SqlParameter("@Active", chbCanLogin.Checked),
                   new SqlParameter("@PassHint",txtPass.Text));
                if (i == 1)
                {
                    ShowMessageBox("Cập nhật thành công");
                }
                else
                {
                    ShowMessageBox("Cập nhật thất bại");
                }
            
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }   
    }
    
    void GetBrandList()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getBrandList", CommandType.StoredProcedure);
        dt.BindDropDownList(db, ddlChiNhanh, "Sitecode", "sitename");

    }
    void GetUserType()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getUserTypeList", CommandType.StoredProcedure);
        dt.BindDropDownList(db, ddlThuocNhom, "TypeID", "TypeName");
    }
    void BindGridSearch()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "[K_FindUserSystem]", CommandType.StoredProcedure,
            new SqlParameter("@parameter", txtSearch.Text));

        if (db.Rows.Count > 0)
        {
            grvUser.DataSource = db;
        }
        else
        {
            grvUser.DataSource = null;
        }
        grvUser.DataBind();

    }
    protected void grvUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvUser.PageIndex = e.NewPageIndex;
        BindGridSearch();

    }
    protected void grvUser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
          dt = new ClassExecute.ClassExecute();
          try
          {
              if (e.CommandName == "chon")
              {

                  GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                  Label lblUser = (Label)grv.FindControl("lblUser");
                  DataTable db = dt.Get_Data_DataTable(str, ref error, "[K_GetUser]", CommandType.StoredProcedure,
                      new SqlParameter("@UserName", lblUser.Text));

                  txtName.Text = db.Rows[0]["UserDescription"].ToString();
                  txtUserName.Text = db.Rows[0]["UserName"].ToString();
                  chbCanLogin.Checked = bool.Parse(db.Rows[0]["Active"].ToString());
                  txtPass.Text = db.Rows[0]["PassHint"].ToString();
                  try
                  {
                      ddlChiNhanh.SelectedValue = db.Rows[0]["Sitecode"].ToString();
                  }
                  catch
                  {
                  }
                  try
                  {
                      ddlThuocNhom.SelectedValue = db.Rows[0]["UserType"].ToString();
                  }
                  catch
                  {
                  }
                  grvUser.DataSource = null;
                  grvUser.DataBind();

                  TabContainer1.ActiveTabIndex = 0;
              }
          }
          catch (Exception ex)
          {

              string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
              dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
          }    
    }
}